package com.arron.prototype;

import com.arron.prototype.deepclone.DeepCloneableTarget;
import com.arron.prototype.deepclone.DeepPrototype;

/*
原型模式的注意事项和细节
1) 创建新的对象比较复杂时，可以利用原型模式简化对象的创建过程，同时也能够提高效率
2) 不用重新初始化对象，而是动态地获得对象运行时的状态
3) 如果原始对象发生变化(增加或者减少属性)，其它克隆对象的也会发生相应的变化，无需修改代码
4) 在实现深克隆的时候可能需要比较复杂的代码
5) 缺点：需要为每一个类配备一个克隆方法，这对全新的类来说不是很难，但对已有的类进行改造时，需要修改其源代码，违背了ocp原则，这点请同学们注意.
 */
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {


        /*
        ////如果添加一个属性，客户端需要修改
        Sheep sheep =new Sheep("tom",1,"白色");
        Sheep sheep2 = new Sheep(sheep.getName(),sheep.getAge(),sheep.getColor());
        Sheep sheep3 = new Sheep(sheep.getName(),sheep.getAge(),sheep.getColor());
        Sheep sheep4 = new Sheep(sheep.getName(),sheep.getAge(),sheep.getColor());
        Sheep sheep5 = new Sheep(sheep.getName(),sheep.getAge(),sheep.getColor());
        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);
        System.out.println(sheep5);
         */


        /*
        System.out.println("~原型模式完成对象创建 浅拷贝~");
        //如果添加一个属性，客户端不需要修改
        Sheep sheep = new Sheep("tom", 1, "白色");
        Sheep sheepF = new Sheep("jack", 2, "黑色");
        sheep.setFriend(sheepF);
        //浅拷贝
        Sheep sheep1 = (Sheep) sheep.clone();
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        Sheep sheep4 = (Sheep) sheep.clone();
        Sheep sheep5 = (Sheep) sheep.clone();
        System.out.println(sheep);
        System.out.println("sheep2 = " + sheep2 + " sheep2.friend=" + sheep2.getFriend().hashCode());
        System.out.println("sheep3 = " + sheep3 + " sheep3.friend=" + sheep3.getFriend().hashCode());
        System.out.println("sheep4 = " + sheep4 + " sheep4.friend=" + sheep4.getFriend().hashCode());
        System.out.println("sheep5 = " + sheep5 + " sheep5.friend=" + sheep5.getFriend().hashCode());
         */


        //方式1 clone 深拷贝
        /*
        System.out.println("~方式1 clone深拷贝~");
        DeepPrototype p =new DeepPrototype();
        p.name="宋江";
        p.deepCloneableTarget = new DeepCloneableTarget("大牛","大牛的类");
        DeepPrototype p2 = (DeepPrototype) p.clone();
        System.out.println("p.name = " + p.name +" p.deepCloneableTarget="+p.deepCloneableTarget.hashCode());
        System.out.println("p2.name = " + p2.name +" p2.deepCloneableTarget="+p2.deepCloneableTarget.hashCode());
        */

        //方式2 序列化深拷贝
        System.out.println("~方式2 序列化深拷贝~");
        DeepPrototype p = new DeepPrototype();
        p.name = "宋江";
        p.deepCloneableTarget = new DeepCloneableTarget("大牛", "大牛的类");
        DeepPrototype p2 = (DeepPrototype) p.deepClone();
        System.out.println("p.name = " + p.name + " p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
        System.out.println("p2.name = " + p2.name + " p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());
    }
}
